package me.zixing.java.chapter08.task01;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by bingh on 2017/11/10.
 */
public class StudentManager {

    private static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args){
        System.out.println("学生管理系统");
        System.out.println("1.添加学生"+"\t\t"+"2.列出学生信息");

        while(true){
            System.out.println("请选择(输入0退出系统)：");
            int option = scanner.nextInt();
            switch (option){
                case 0:System.exit(0);
                case 1:addStudent();break;
                case 2:listStudents();break;
                default:System.out.println("输入有误");
            }
        }

    }

    public static void addStudent(){

        System.out.println("请输入学生学号:");
        String studentNo = scanner.next();
        System.out.println("请输入学生姓名:");
        String studentName = scanner.next();
        System.out.println("请输入学生年龄:");
        int studentAge = scanner.nextInt();

        Student student = new Student();
        try{
            student.setStudentNo(studentNo);
            student.setStudentName(studentName);
            student.setStudentAge(studentAge);

            Object2FileUtil.writeObjectToFile(student);

        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("写入成功");
    }

    public static void listStudents(){
        List<Student> students = Object2FileUtil.readObjectFromFile(Student.class);
        for (Student student:students) {
            System.out.println("学号:"+student.getStudentNo()+"\t"+
                                "姓名:"+student.getStudentName()+"\t\t"+
                                "年龄:"+student.getStudentAge()
            );
        }
    }


}
